package smartclass.fragment;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import smartclass.adapter.ExpandListDirectionAdapter;
import smartclass.adapter.ExpandListDocumentAdapter;
import smartclass.adapter.ExpandListHomeworkAdapter;
import smartclass.adapter.ExpandListTestAdapter;
import smartclass.adapter.ExpandListTestStudentAdapter;
import smartclass.bussiness.DirectionBussiness;
import smartclass.bussiness.DocumentBussiness;
import smartclass.bussiness.HomeworkBussiness;
import smartclass.bussiness.TestBussiness;
import smartclass.bussiness.TestStudentBussiness;
import smartclass.bussiness.TopicBussiness;
import smartclass.data.FinalValues;
import smartclass.entity.DirectionInfo;
import smartclass.entity.DocumentInfo;
import smartclass.entity.HomeworkInfo;
import smartclass.entity.TestInfo;
import smartclass.entity.TestStudentInfo;
import smartclass.entity.TopicInfo;
import smartclass.present.LogInActivity;
import smartclass.utils.Helper;
import android.annotation.SuppressLint;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageView;
import android.widget.TableRow;
import android.widget.TextView;

import com.jeremyfeinstein.slidingmenu.example.R;

@SuppressLint("ValidFragment")
public class TeacherFolderFragment extends Fragment implements OnClickListener {

	private Context context;
	private int idTeacher;
	private String name;
	private DirectionBussiness directionBussiness;
	private DocumentBussiness documentBussiness;
	private HomeworkBussiness homeworkBussiness;
	private TestBussiness testBussiness;
	private TestStudentBussiness testStudentBussiness;
	private TopicBussiness topicBussiness;
	public static int totalHeightListDocument, totalHeightListDirection,
			totalHeightListHomework, totalHeightListTest,
			totalHeightListTestStudent;

	// Group List
	private List<TopicInfo> groupListDocument, groupListHomework,
			groupListDirection;
	private List<String> groupListTest, groupListTestStudent;

	// Group Child
	private List<DocumentInfo> childListDocument;
	private List<HomeworkInfo> childListHomework;
	private List<DirectionInfo> childListDirection;
	private List<TestInfo> childListTest;
	private List<TestStudentInfo> childListTestStudent;

	// Map Group-Child

	private Map<TopicInfo, List<DocumentInfo>> documentCollection;
	private Map<TopicInfo, List<HomeworkInfo>> homeworkCollection;
	private Map<TopicInfo, List<DirectionInfo>> directionCollection;
	private Map<String, List<TestInfo>> testCollection;
	private Map<String, List<TestStudentInfo>> testStudentCollection;

	// Adapter expandList

	private ExpandListDirectionAdapter expandListDirectionAdapter;
	private ExpandListDocumentAdapter expandListDocumentAdapter;
	private ExpandListHomeworkAdapter expandListHomeworkAdapter;
	private ExpandListTestStudentAdapter expandListTestStudentAdapter;
	private ExpandListTestAdapter expandListAdapterTest;

	// View
	private ExpandableListView expandListDocument, expandListHomework,
			expandListDirection, expandListTest, expandListTestStudent;
	private ImageView btnAddTopic, btnAddTest;
	private TextView tvLogOut, tvUpdateInformation;

	public TeacherFolderFragment(Context context, int idTeacher) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.idTeacher = idTeacher;

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		topicBussiness = TopicBussiness.getInstance();
		directionBussiness = DirectionBussiness.getInstance();
		documentBussiness = DocumentBussiness.getInstance();
		homeworkBussiness = HomeworkBussiness.getInstance();
		testBussiness = TestBussiness.getInstance();
		testStudentBussiness = TestStudentBussiness.getInstance();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View v = inflater.inflate(R.layout.teacher_fragment_folder_manager,
				container, false);
		tvLogOut = (TextView) v.findViewById(R.id.logOutTeacher);
		tvLogOut.setOnClickListener(this);
		tvUpdateInformation = (TextView) v
				.findViewById(R.id.updateInformationTeacher);
		tvUpdateInformation.setOnClickListener(this);
		createGroupList();
		createCollecttion();

		FinalValues
				.displayNotification(context, testCollection.size() + " :size");
		btnAddTopic = (ImageView) v.findViewById(R.id.addTopic);
		btnAddTopic.setOnClickListener(this);
		btnAddTest = (ImageView) v.findViewById(R.id.addTest);
		btnAddTest.setOnClickListener(this);
		expandListDirection = (ExpandableListView) v
				.findViewById(R.id.listDirectionTeacherManager);
		expandListDocument = (ExpandableListView) v
				.findViewById(R.id.listDocumentTeacherManager);
		expandListHomework = (ExpandableListView) v
				.findViewById(R.id.listHomeworkTeacherManager);
		expandListTest = (ExpandableListView) v
				.findViewById(R.id.listTestTeacherManager);
		expandListTestStudent = (ExpandableListView) v
				.findViewById(R.id.listTestStudentTeacherManager);

		// ------------------List Document------------------//
		expandListDocumentAdapter = new ExpandListDocumentAdapter(context,
				groupListDocument, documentCollection, expandListDocument);
		expandListDocument.setAdapter(expandListDocumentAdapter);
		totalHeightListDocument = Helper.getListViewSize(expandListDocument);
		expandListDocumentAdapter.notifyDataSetChanged();

		expandListDocument.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				// TODO Auto-generated method stub
				final DocumentInfo selected = (DocumentInfo) expandListDocumentAdapter
						.getChild(groupPosition, childPosition);
				FinalValues.displayNotification(context, selected.getId() + " ");
				return false;
			}
		});

		expandListDocument
				.setOnGroupExpandListener(new OnGroupExpandListener() {

					@Override
					public void onGroupExpand(int groupPosition) {
						// TODO Auto-generated method stub
						FinalValues.displayNotification(context, groupPosition
								+ " ");
						documentBussiness.updateHashMapCheckListExpand(
								groupPosition, true);
						totalHeightListDocument = Helper.groupExpand(
								expandListDocumentAdapter, expandListDocument,
								groupPosition, totalHeightListDocument);
						if (expandListDocumentAdapter
								.getChildrenCount(groupPosition) == 0) {
							FinalValues.displayNotification(context,
									"No have any item");
						}
					}
				});
		expandListDocument
				.setOnGroupCollapseListener(new OnGroupCollapseListener() {

					@Override
					public void onGroupCollapse(int groupPosition) {
						// TODO Auto-generated method stub
						documentBussiness.updateHashMapCheckListExpand(
								groupPosition, false);
						totalHeightListDocument = Helper.groupCollapse(
								expandListDocumentAdapter, expandListDocument,
								groupPosition, totalHeightListDocument);
					}
				});
		// -----------------------------List Homework--------------------//
		expandListHomeworkAdapter = new ExpandListHomeworkAdapter(context,
				groupListHomework, homeworkCollection);
		expandListHomework.setAdapter(expandListHomeworkAdapter);
		totalHeightListHomework = Helper.getListViewSize(expandListHomework);
		expandListHomeworkAdapter.notifyDataSetChanged();

		expandListHomework.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				// TODO Auto-generated method stub
				final HomeworkInfo home = (HomeworkInfo) expandListHomeworkAdapter
						.getChild(groupPosition, childPosition);
				FinalValues.displayNotification(context, home.getName());
				return false;
			}
		});

		expandListHomework
				.setOnGroupExpandListener(new OnGroupExpandListener() {

					@Override
					public void onGroupExpand(int groupPosition) {
						// TODO Auto-generated method stub
						totalHeightListHomework = Helper.groupExpand(
								expandListHomeworkAdapter, expandListHomework,
								groupPosition, totalHeightListHomework);

					}
				});
		expandListHomework
				.setOnGroupCollapseListener(new OnGroupCollapseListener() {

					@Override
					public void onGroupCollapse(int groupPosition) {
						// TODO Auto-generated method stub
						totalHeightListHomework = Helper.groupCollapse(
								expandListHomeworkAdapter, expandListHomework,
								groupPosition, totalHeightListHomework);
					}
				});

		// ---------------------------List Direction-------------------------
		expandListDirectionAdapter = new ExpandListDirectionAdapter(context,
				groupListDirection, directionCollection);
		expandListDirection.setAdapter(expandListDirectionAdapter);
		totalHeightListDirection = Helper.getListViewSize(expandListDirection);
		expandListDirectionAdapter.notifyDataSetChanged();

		expandListDirection.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				// TODO Auto-generated method stub
				final DirectionInfo direction = (DirectionInfo) expandListDirectionAdapter
						.getChild(groupPosition, childPosition);
				FinalValues.displayNotification(context, direction.getName());
				return false;
			}
		});

		expandListDirection
				.setOnGroupExpandListener(new OnGroupExpandListener() {

					@Override
					public void onGroupExpand(int groupPosition) {
						// TODO Auto-generated method stub
						totalHeightListDirection = Helper.groupExpand(
								expandListDirectionAdapter,
								expandListDirection, groupPosition,
								totalHeightListDirection);
					}
				});
		expandListDirection
				.setOnGroupCollapseListener(new OnGroupCollapseListener() {

					@Override
					public void onGroupCollapse(int groupPosition) {
						// TODO Auto-generated method stub
						totalHeightListDirection = Helper.groupCollapse(
								expandListDirectionAdapter,
								expandListDirection, groupPosition,
								totalHeightListDirection);
					}
				});

		// ---------------------list Test--------------------------------//
		expandListAdapterTest = new ExpandListTestAdapter(context,
				groupListTest, testCollection);
		expandListTest.setAdapter(expandListAdapterTest);
		totalHeightListTest = Helper.getListViewSize(expandListTest);
		expandListAdapterTest.notifyDataSetChanged();

		expandListTest.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				// TODO Auto-generated method stub
				final TestInfo test = (TestInfo) expandListAdapterTest
						.getChild(groupPosition, childPosition);
				FinalValues.displayNotification(context, test.getName());
				return false;
			}
		});

		expandListTest.setOnGroupExpandListener(new OnGroupExpandListener() {

			@Override
			public void onGroupExpand(int groupPosition) {
				// TODO Auto-generated method stub
				totalHeightListTest = Helper.groupExpand(expandListAdapterTest,
						expandListTest, groupPosition, totalHeightListTest);
				if (expandListAdapterTest.getChildrenCount(groupPosition) == 0) {
					FinalValues.displayNotification(context, "No have any item");
				}
			}
		});
		expandListTest
				.setOnGroupCollapseListener(new OnGroupCollapseListener() {

					@Override
					public void onGroupCollapse(int groupPosition) {
						// TODO Auto-generated method stub
						totalHeightListTest = Helper.groupCollapse(
								expandListAdapterTest, expandListTest,
								groupPosition, totalHeightListTest);
					}
				});
		// ----------------------------List Test Student---------------------//
		expandListTestStudentAdapter = new ExpandListTestStudentAdapter(
				context, groupListTestStudent, testStudentCollection);
		expandListTestStudent.setAdapter(expandListTestStudentAdapter);
		totalHeightListTestStudent = Helper
				.getListViewSize(expandListTestStudent);
		expandListTestStudentAdapter.notifyDataSetChanged();

		expandListTestStudent
				.setOnChildClickListener(new OnChildClickListener() {

					@Override
					public boolean onChildClick(ExpandableListView parent,
							View v, int groupPosition, int childPosition,
							long id) {
						// TODO Auto-generated method stub
						final TestStudentInfo testStudent = (TestStudentInfo) expandListTestStudentAdapter
								.getChild(groupPosition, childPosition);
						FinalValues.displayNotification(context,
								testStudent.getName());
						return false;
					}
				});

		expandListTestStudent
				.setOnGroupExpandListener(new OnGroupExpandListener() {

					@Override
					public void onGroupExpand(int groupPosition) {
						// TODO Auto-generated method stub
						totalHeightListTestStudent = Helper.groupExpand(
								expandListTestStudentAdapter,
								expandListTestStudent, groupPosition,
								totalHeightListTestStudent);
					}
				});
		expandListTestStudent
				.setOnGroupCollapseListener(new OnGroupCollapseListener() {

					@Override
					public void onGroupCollapse(int groupPosition) {
						// TODO Auto-generated method stub
						totalHeightListTestStudent = Helper.groupCollapse(
								expandListTestStudentAdapter,
								expandListTestStudent, groupPosition,
								totalHeightListTestStudent);
					}
				});

		return v;
	}

	// ---------------------------init
	// values----------------------------------------//
	private void createGroupList() {

		/*
		 * init array group list
		 */
		groupListDirection = new ArrayList<TopicInfo>();
		groupListDocument = new ArrayList<TopicInfo>();
		groupListHomework = new ArrayList<TopicInfo>();
		groupListTest = new ArrayList<String>();
		groupListTestStudent = new ArrayList<String>();

		/*
		 * get Item
		 */
		groupListDirection = topicBussiness.getAllTopic();
		groupListDocument = topicBussiness.getAllTopic();
		groupListHomework = topicBussiness.getAllTopic();

		groupListTest.add("15 minutes");
		groupListTest.add("45 minutes");
		groupListTest.add("8 weeks");
		groupListTest.add("Semester");

		groupListTestStudent.add("Class 1");
		groupListTestStudent.add("Class 2");
		groupListTestStudent.add("Class 3");
		groupListTestStudent.add("Class 4");

	}

	private void createCollecttion() {
		/*
		 * init Colection
		 */
		documentCollection = new LinkedHashMap<TopicInfo, List<DocumentInfo>>();
		directionCollection = new LinkedHashMap<TopicInfo, List<DirectionInfo>>();
		homeworkCollection = new LinkedHashMap<TopicInfo, List<HomeworkInfo>>();
		testCollection = new LinkedHashMap<String, List<TestInfo>>();
		testStudentCollection = new LinkedHashMap<String, List<TestStudentInfo>>();

		/*
		 * int arrayChild
		 */
		childListDocument = new ArrayList<DocumentInfo>();
		childListDirection = new ArrayList<DirectionInfo>();
		childListHomework = new ArrayList<HomeworkInfo>();
		childListTest = new ArrayList<TestInfo>();
		childListTestStudent = new ArrayList<TestStudentInfo>();

		/*
		 * int values collection
		 */
		for (TopicInfo topic : groupListDocument) {

			childListDocument = documentBussiness.getAllDocument(topic.getId());
			documentCollection.put(topic, childListDocument);
			childListHomework = homeworkBussiness.getAllHomework(topic.getId());
			homeworkCollection.put(topic, childListHomework);
			childListDirection = directionBussiness.getAllDirection(topic
					.getId());
			directionCollection.put(topic, childListDirection);
		}

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.addTopic:
			FinalValues.displayNotification(context, "Add");
			creatDialog("Add New Topic", 1);
			break;
		case R.id.addTest:
			creatDialog("Add New Test", 2);
			break;
		case R.id.logOutTeacher:
			FinalValues.displayNotification(context, "Log out");
			Intent intent = new Intent(context, LogInActivity.class);
			startActivity(intent);
			((Activity) context).finish();
			break;
		case R.id.updateInformationTeacher:
			break;
		default:
			break;
		}
	}

	public void creatDialog(String title, final int type) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(R.layout.add_new_item_dialog, null);
		builder.setView(v);
		builder.setTitle(title);
		builder.setIcon(R.drawable.gnome_mime_application_x_class_file);
		TableRow tableTestType = (TableRow) v
				.findViewById(R.id.tablerowTestType);
		final EditText edtName = (EditText) v.findViewById(R.id.nameNewItem);

		switch (type) {
		case 1:
			tableTestType.setVisibility(View.GONE);
			break;

		case 2:
			tableTestType.setVisibility(View.VISIBLE);
			break;

		default:
			break;
		}
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				name = edtName.getText().toString();
				if (name.isEmpty()) {
					FinalValues.displayNotification(context,
							"Please, Enter your name file");
					return;
				}
				switch (type) {
				case 1:
					// Add new Topic

					TopicInfo topic = new TopicInfo();
					topic.setName(name);
					topic.setIdTeacher(idTeacher);
					topic.setDateCreate(String.valueOf(System
							.currentTimeMillis()));
					topicBussiness.addTopic(topic);
					updateTopic(groupListDocument, topicBussiness);
					updateTopic(groupListDirection, topicBussiness);
					updateTopic(groupListHomework, topicBussiness);
					for (TopicInfo t : groupListDocument) {
						Log.e("Group List", t.getName());
					}

					break;
				case 2:
					// Add new Test

					break;
				default:
					break;
				}
			}
		});
		builder.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				});
		builder.create().show();
	}

	private void updateTopic(List<TopicInfo> groupList,
			TopicBussiness topicBussiness) {
		groupList.clear();
		Log.e("Topic", "Here");
		for (TopicInfo topic : topicBussiness.getAllTopic()) {
			groupList.add(topic);
			Log.e("Topic", topic.getName());
		}
		totalHeightListDocument = Helper.getListViewSize(expandListDocument);
		totalHeightListDirection = Helper.getListViewSize(expandListDirection);
		totalHeightListHomework = Helper.getListViewSize(expandListHomework);
	}
}
